package com.example.books.ui.home;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.books.CollectionActivity;
import com.example.books.HomeActivity;
import com.example.books.MainActivity;
import com.example.books.R;
import com.example.books.ReadActivity;
import com.example.books.adapter.BookAdapter;
import com.example.books.data.Book;
import com.example.books.data.Collection;
import com.example.books.data.User;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class HomeFragment extends Fragment implements View.OnClickListener {

    private HomeViewModel homeViewModel;
    private View root;
    private Button collectionBtn;
    private EditText greetText;
    private ListView listBook;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel = ViewModelProviders.of(getActivity()).get(HomeViewModel.class);
        root = inflater.inflate(R.layout.activity_home, container, false);
        initView();
        return root;
    }

    private void initView() {
        collectionBtn = (Button) root.findViewById(R.id.collectionBtn);
        collectionBtn.setOnClickListener(this);
        greetText = (EditText) root.findViewById(R.id.greetText);
        listBook = (ListView) root.findViewById(R.id.listBook);
        collectionB();
        toRead();
        BookAdapter adapter = new BookAdapter(homeViewModel.getallB(),getContext(),homeViewModel.getB());
        listBook.setAdapter(adapter);

    }
    private void toRead(){
        listBook.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getContext(), ReadActivity.class);
                intent.putExtra("path", homeViewModel.getallB().get(i).getBookpath());
                startActivity(intent);
            }
        });

    }
    private void collectionB(){
        listBook.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                Collection collection = new Collection();
                collection.setBookId( homeViewModel.getallB().get(i).getId());
                collection.setUserId(homeViewModel.getId());
                boolean save = collection.save();
                ImageView imgeView = view.findViewById(R.id.imgColletion);
                imgeView.setSelected(true);
                Toast.makeText(getContext(), homeViewModel.getTrue(save), Toast.LENGTH_SHORT).show();
                return true;
            }
        });

    }
    @Override
    public void onClick(View view) {
        Set<Book> books = homeViewModel.getallS(greetText.getText().toString());
        //转换
        List<Book> books1=new ArrayList<>();
        for (Book b : books) {
            books1.add(b);
        }
        BookAdapter adapter = new BookAdapter(books1,getContext(),homeViewModel.getB());
        listBook.setAdapter(adapter);
    }
}